<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      const arr = [
        {
          select: "33",
          nid: "3581",
          name: "22",
          title: "11",
          tid: "3579",
        },
        {
          select: "333",
          nid: "3581",
          name: "22",
          title: "11",
          tid: "3579",
        },
        {
          select: "123",
          nid: "3581",
          name: "22",
          title: "11",
          tid: "3579",
        },
        {
          select: "hjk",
          nid: "3586",
          name: "asdf",
          title: "11",
          tid: "3579",
        },
        {
          select: "sdf",
          nid: "3586",
          name: "asdf",
          title: "11",
          tid: "3579",
        },
        {
          select: "sg",
          nid: "3589",
          name: "smgui",
          title: "1",
          tid: "3576",
        },
        {
          select: "g",
          nid: "3589",
          name: "smgui",
          title: "1",
          tid: "3576",
        },
      ];
      // 将这样一个数组，变成能够以树形展示的对象

      //先将数组中的每一个节点放到temp对象中（创建set）
      //即数组中有{id: '2-3', parent_id: '2',...}这样一个节点，需要将他放到temp中变成 '2-3': {id: '2-3', parent_id: '2',...}这种JSON结构
      //直接遍历整个temp对象，通过这句代码   temp[temp[i].parent_id].children[temp[i].id] = temp[i];
      //将当前子节点与父节点建立连接。是因为我们保证了父节点一定在子节点前，
      //那么当子节点出现的时候就直接可以用temp[temp[i].parent_id]来查找到父节点这个时候先父节点的children对象中添加一个引用即可。
      let cc = {};

      for (let i = 0; i < arr.length; i++) {
        const { select, nid, name, title, tid } = arr[i];
        const objNidTmp = {
          name,
          nid,
          select: [select],
        };
        if (nid in cc) {
          cc[nid].select.push(select); //绑定浅拷贝，objNidTmp也改变了
          continue;
        }
        cc[nid] = objNidTmp;
        // objNidTmp;
        console.log(cc);
        console.log(objNidTmp);
      }

      function func(arrOri) {
        const objTid = {},
          objNid = {};

        const arrRes = [];
        for (let i = 0; i < arrOri.length; i++) {
          const { select, nid, name, title, tid } = arrOri[i];
          if (nid in objNid) {
            objNid[nid].select.push(select);
            continue;
          }
          const objNidTmp = {
            name,
            nid,
            select: [select],
          };
          console.log(objNidTmp);
          objNid[nid] = objNidTmp;
          // console.log(objNidTmp);
          if (!(tid in objTid)) {
            const objTidTmp = {
              title,
              tid,
              contentArr: [objNidTmp],
            };
            objTid[tid] = objTidTmp;
            arrRes.push(objTidTmp);
          } else objTid[tid].contentArr.push(objNidTmp);
        }
        return arrRes;
      }
      // const obj = buildTree(arr, "tid", "nid");
      console.log(func(arr));
    </script>
  </body>
</html>
